/**
 * Copyright (c) 2016, smuyyh@gmail.com All Rights Reserved.
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG             #
 * #                                                   #
 */
package com.lhwalq.books.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.lhwalq.books.base.BaseActivity;
import com.lhwalq.books.base.Constant;
import com.lhwalq.books.component.AppComponent;
import com.lhwalq.books.component.DaggerMainComponent;
import com.lhwalq.books.R;
import com.lhwalq.books.ui.fragment.RecommendFragment;
import com.lhwalq.books.ui.presenter.MainActivityPresenter;
import com.lhwalq.books.utils.SharedPreferencesUtil;

import net.youmi.android.normal.banner.BannerManager;
import net.youmi.android.normal.banner.BannerViewListener;
import net.youmi.android.normal.spot.SpotListener;
import net.youmi.android.normal.spot.SpotManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * https://github.com/JustWayward/BookReader
 */
public class MainActivity extends BaseActivity {

    @Bind(R.id.viewpager)
    ViewPager mViewPager;

    private List<Fragment> mTabContents;
    private FragmentPagerAdapter mAdapter;

    @Bind(R.id.fl_ad_money)
    FrameLayout flAdMoney;

    @Inject
    MainActivityPresenter mPresenter;

    private Random random;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initDatas();
        configViews();

        if (SharedPreferencesUtil.getInstance().getBoolean(Constant.IS_FIRST_OPEN_APP, true)) {
            SharedPreferencesUtil.getInstance().putBoolean(Constant.IS_FIRST_OPEN_APP, false);
        } else {
//            setupNativeSpotAd();
        }
    }

    public void showSpotAd() {
        if (null == random) {
            random = new Random();
        }
        int randomInt = random.nextInt(4);
        if (2 < randomInt) {
            // 展示轮播插屏广告
            SpotManager.getInstance(MainActivity.this).showSlideableSpot(MainActivity.this, new SpotListener() {

                @Override
                public void onShowSuccess() {
                }

                @Override
                public void onShowFailed(int errorCode) {
                }

                @Override
                public void onSpotClosed() {
                }

                @Override
                public void onSpotClicked(boolean isWebPage) {
                }
            });
        }
    }

    /**
     * 悬浮布局
     */
    public void setupNativeSpotAd() {

        // 设置插屏图片类型，默认竖图
        //		// 横图
        //		SpotManager.getInstance(mContext).setImageType(SpotManager
        // .IMAGE_TYPE_HORIZONTAL);
        // 竖图
        SpotManager.getInstance(this).setImageType(SpotManager.IMAGE_TYPE_VERTICAL);

        // 设置动画类型，默认高级动画
        //		// 无动画
        //		SpotManager.getInstance(mContext).setAnimationType(SpotManager
        // .ANIMATION_TYPE_NONE);
        //		// 简单动画
        //		SpotManager.getInstance(mContext).setAnimationType(SpotManager
        // .ANIMATION_TYPE_SIMPLE);
        // 高级动画
        SpotManager.getInstance(this).setAnimationType(SpotManager.ANIMATION_TYPE_ADVANCED);

        // 实例化LayoutParams
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置广告条的悬浮位置，这里示例为右下角
        layoutParams.gravity = Gravity.BOTTOM;
        // 获取广告条控件
        final View bannerView = BannerManager.getInstance(this).getBannerView(new BannerViewListener
                        () {

                    @Override
                    public void onRequestSuccess() {}

                    @Override
                    public void onSwitchBanner() {}

                    @Override
                    public void onRequestFailed() {}
                });
        flAdMoney.addView(bannerView, layoutParams);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void initToolBar() {
        mCommonToolbar.setLogo(R.mipmap.logo);
        setTitle("");
    }

    @Override
    public void initDatas() {
        mTabContents = new ArrayList<>();
        mTabContents.add(new RecommendFragment());

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mTabContents.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mTabContents.get(position);
            }
        };
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        showSpotAd();
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void configViews() {
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 插播广告
        SpotManager.getInstance(this).onDestroy();
    }

    @Override
    public void onBackPressed() {
        // 如果有需要，可以点击后退关闭插播广告。
        if (SpotManager.getInstance(this).isSlideableSpotShowing()) {
            SpotManager.getInstance(this).hideSlideableSpot();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        // 插播广告
        SpotManager.getInstance(this).onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 插播广告
        SpotManager.getInstance(this).onPause();
    }
}